<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable" @row-click="handleRowClick">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'cardd:order:carddYcdd:edit'">
          <Icon icon="i-fluent:add-12-filled" />
          {{ t('新增') }}
        </a-button>
        <a-button type="primary" @click="handleOrderCancel()" v-auth="'cardd:order:carddYcdd:edit'">
          <Icon icon="" />
          {{ t('订单取消') }}
        </a-button>
        <a-button
          type="primary"
          @click="handleOrderRecover()"
          v-auth="'cardd:order:carddYcdd:edit'"
        >
          <Icon icon="" />
          {{ t('订单恢复') }}
        </a-button>
        <!--        <a-button type="default" @click="handleExport()">-->
        <!--          <Icon icon="i-ant-design:download-outlined" /> {{ t('导出') }}-->
        <!--        </a-button>-->
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.ycddDdbh }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsCarddOrderCarddYcddList">
  import { unref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicColumn, BasicTable, useTable } from '/@/components/Table';
  import {
    carddYcddCancel,
    carddYcddDelete,
    carddYcddListData,
    carddYcddRecover,
  } from '/@/api/cardd/order/carddYcdd';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import FormImport from './formImport.vue';
  import { coreYwlxTreeData } from '/@/api/bas/ywlx/coreYwlx';

  const { t } = useI18n('cardd.order.carddYcdd');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('用车订单管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 8, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('订单状态'),
        field: 'ycddDdzt',
        component: 'Select',
        componentProps: {
          dictType: 'car_order_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('订单编号'),
        field: 'ycddDdbh',
        component: 'Input',
      },
      {
        label: t('用车单位'),
        field: 'basCus.khdaDwjc',
        component: 'Input',
      },
      {
        label: t('部门'),
        field: 'ycddBm',
        component: 'Input',
      },
      {
        label: t('联系人'),
        field: 'contact.khlxrLxrmc',
        component: 'Input',
      },
      {
        label: t('业务类型'),
        field: 'ycddYwlx',
        component: 'TreeSelect',
        componentProps: {
          api: coreYwlxTreeData,
          showSearch: false,
        },
      },
      {
        label: t('用车时间'),
        field: 'ycddYckssj',
        component: 'RangePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },

      {
        label: t('结算方式'),
        field: 'ycddJsfs',
        component: 'Select',
        componentProps: {
          dictType: 'car_settle_tpye',
          showSearch: false,
          allowClear: true,
        },
      },
      {
        label: t('专项名称'),
        field: 'coreZxda.zxdaZxmc',
        component: 'Input',
      },
      {
        label: t('车辆要求'),
        field: 'ycddClyq',
        component: 'Input',
      },
      {
        label: t('车辆行程'),
        field: 'ycddClxc',
        component: 'Input',
      },
      // {
      //   label: t('创建人'),
      //   field: 'createByName',
      //   component: 'Input',
      // },
    ],
    fieldMapToTime: [['ycddYckssj', ['useDateBegin', 'useDateEnd']]],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('订单状态'),
      dataIndex: 'ycddDdzt',
      key: 'a.ycdd_ddzt',
      dictType: 'car_order_status',
      sorter: true,
      width: 80,
      align: 'center',
    },
    {
      title: t('订单编号'),
      dataIndex: 'ycddDdbh',
      key: 'a.ycdd_ddbh',
      sorter: true,
      width: 120,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('用车单位'),
      dataIndex: 'basCus.khdaDwjc',
      key: 'cus.khda_dwjc',
      sorter: true,
      width: 80,
      align: 'left',
    },
    {
      title: t('部门'),
      dataIndex: 'ycddBm',
      key: 'a.ycdd_bm',
      sorter: true,
      width: 80,
      align: 'left',
    },
    {
      title: t('联系人'),
      // dataIndex: 'ycddLxr',
      dataIndex: 'contact.khlxrLxrmc',
      key: 'a.ycdd_lxr',
      sorter: true,
      width: 80,
      align: 'left',
    },
    {
      title: t('联系电话'),
      dataIndex: 'ycddLxdh',
      key: 'a.ycdd_lxdh',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('车辆行程'),
      dataIndex: 'ycddClxc',
      key: 'a.ycdd_clxc',
      sorter: true,
      width: 120,
      align: 'left',
    },
    {
      title: t('上车地点'),
      dataIndex: 'ycddScdd',
      key: 'a.ycdd_scdd',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('下车地点'),
      dataIndex: 'ycddXcdd',
      key: 'a.ycdd_xcdd',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('付款方式'),
      dataIndex: 'ycddFkfs',
      key: 'a.ycdd_fkfs',
      sorter: true,
      width: 80,
      align: 'left',
    },
    {
      title: t('任务编号'),
      dataIndex: 'ycddRwbh',
      key: 'a.ycdd_rwbh',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('任务等级'),
      dataIndex: 'ycddRwdj',
      key: 'a.ycdd_rwdj',
      sorter: true,
      width: 80,
      align: 'left',
    },
    {
      title: t('业务类型'),
      dataIndex: 'coreYwlx.ywlxLxmc',
      key: 'a.ycdd_ywlx',
      sorter: true,
      width: 80,
      align: 'left',
    },
    {
      title: t('整单折扣'),
      dataIndex: 'ycddZdzk',
      key: 'a.ycdd_zdzk',
      sorter: true,
      width: 80,
      align: 'right',
    },
    {
      title: t('用车开始时间'),
      dataIndex: 'ycddYckssj',
      key: 'a.ycdd_yckssj',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('用车结束时间'),
      dataIndex: 'ycddYcjssj',
      key: 'a.ycdd_ycjssj',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('租车时长'),
      dataIndex: 'ycddZcsz',
      key: 'a.ycdd_zcsz',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('订单押金'),
      dataIndex: 'ycddDdyj',
      key: 'a.ycdd_ddyj',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('预收金额'),
      dataIndex: 'ycddYsje',
      key: 'a.ycdd_ysje',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('结算方式'),
      dataIndex: 'ycddJsfs',
      key: 'a.ycdd_jsfs',
      dictType: 'car_settle_tpye',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('专项名称'),
      dataIndex: 'coreZxda.zxdaZxmc',
      key: 'a.ycdd_zxmc',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('车辆要求'),
      dataIndex: 'ycddClyq',
      key: 'a.ycdd_clyq',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('业务员'),
      dataIndex: 'ycddYwy',
      key: 'a.ycdd_ywy',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('更新时间'),
      dataIndex: 'updateDate',
      key: 'a.update_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('创建人'),
      dataIndex: 'createByName',
      key: 'a.create_by_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('修改人'),
      dataIndex: 'updateByName',
      key: 'a.update_by_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('租赁方式'),
      dataIndex: 'btZlfs',
      width: 130,
      align: 'left',
      dictType: 'car_zlfs',
      ifShow: false,
    },
    {
      title: t('品牌型号'),
      dataIndex: 'btPpxh',
      width: 130,
      align: 'left',
      dictType: 'car_type',
      ifShow: false,
    },
    {
      title: t('外调类型'),
      dataIndex: 'btWdlx',
      width: 130,
      align: 'left',
      dictType: 'car_wdlx',
      ifShow: false,
    },
    {
      title: t('是否包干'),
      dataIndex: 'btSfbgj',
      width: 130,
      align: 'left',
      dictType: 'sys_yes_no',
      ifShow: false,
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑用车订单'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'cardd:order:carddYcdd:edit',
      },
      {
        icon: 'i-ant-design:delete-outlined',
        color: 'error',
        title: t('删除用车订单'),
        popConfirm: {
          title: t('是否确认删除用车订单'),
          confirm: handleDelete.bind(this, record),
        },
        auth: 'cardd:order:carddYcdd:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRowKeys }] = useTable({
    api: carddYcddListData,
    beforeFetch: (params) => {
      if (params.useDateBegin && params.useDateEnd) {
        params.useDate_between = params.useDateBegin + ' - ' + params.useDateEnd;
      }
      console.log(params);
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  async function handleDelete(record: Recordable) {
    const params = { id: record.id };
    const res = await carddYcddDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  // 订单取消
  async function handleOrderRecover() {
    if (getSelectRowKeys().length) {
      const res = await carddYcddRecover({ selIds: getSelectRowKeys().join(',') });
      showMessage(res.message);
      handleSuccess({});
    } else {
      showMessage('请先选中要恢复的用车订单！', 'error');
    }
  }

  //订单恢复
  async function handleOrderCancel() {
    if (getSelectRowKeys().length) {
      const res = await carddYcddCancel({ selIds: getSelectRowKeys().join(',') });
      showMessage(res.message);
      handleSuccess({});
    } else {
      showMessage('请先选中要取消的用车订单！', 'error');
    }
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }

  function handleRowClick() {
    console.log('handleRowClick', arguments);
  }
</script>
